/**
 * 
 */
package com.tvt.sexyapp_demo.appconfig.reader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import android.content.Context;

import com.tvt.sexyapp_demo.appconfig.xml.AppConfigXml;
import com.tvt.sexyapp_demo.appconfig.xml.XItem;
import com.tvt.sexyapp_demo.appconfig.xml.XSMSConfig;
import com.tvt.sexyapp_demo.debug.Debug;
import com.tvt.sexyapp_demo.downloader.GNetworkUtils;

/**
 * @author MR_HIEU
 * 
 */
public class AppConfigReader {

	private static final String LOG_TAG = "AppConfigReader";
	private static final String CONFIG_SMS_URL = "http://hunghh.caidat.vn/enduser/app/download/xas1Config";

	private static final String APP_INFO_PATH = "AppInfo.xml";
	private static String APP_INFO = null;

	public static int getMaxPageNumber(AppConfigXml pAppConfig) {

		int NUMBER_OF_ITEMS_PER_PAGE = pAppConfig.getRunningApp().getItemPerPager();

		int NUMBER_OF_ITEMS_ON_CONFIG = pAppConfig.getRunningApp().getItems().size();

		int MAX_PAGE_NUMBER = NUMBER_OF_ITEMS_ON_CONFIG / NUMBER_OF_ITEMS_PER_PAGE;

		if (NUMBER_OF_ITEMS_ON_CONFIG % NUMBER_OF_ITEMS_PER_PAGE != 0)
			MAX_PAGE_NUMBER++;

		return MAX_PAGE_NUMBER;
	}

	public static ArrayList<XItem> readPageItems(AppConfigXml pAppConfig, int pPageNumber) {

		int NUMBER_OF_ITEMS_PER_PAGE = pAppConfig.getRunningApp().getItemPerPager();

		int MAX_PAGE_NUMBER = getMaxPageNumber(pAppConfig);

		if (pPageNumber <= 0 || pPageNumber > MAX_PAGE_NUMBER) {

			Debug.e(LOG_TAG, pPageNumber + " is invalid!");
			return null;
		}

		ArrayList<XItem> items_on_page = new ArrayList<XItem>();

		int firstIndex = (pPageNumber - 1) * NUMBER_OF_ITEMS_PER_PAGE;

		int NUMBER_OF_ITEMS_ON_CONFIG = pAppConfig.getRunningApp().getItems().size();

		int lastIndex = Math.min(firstIndex + NUMBER_OF_ITEMS_PER_PAGE - 1, NUMBER_OF_ITEMS_ON_CONFIG - 1);

		for (int i = firstIndex; i <= lastIndex; i++)
			items_on_page.add(pAppConfig.getRunningApp().getItems().get(i));

		return items_on_page;
	}

	public static AppConfigXml decodeAppConfig(Context pContext, String pXmlAssetPath) {

		InputStream is = null;
		try {
			is = pContext.getAssets().open(pXmlAssetPath);
		} catch (IOException e1) {
			return null;
		}

		if (is != null) {

			AppConfigXml appConfig = null;

			Serializer serializer = new Persister();
			try {
				appConfig = serializer.read(AppConfigXml.class, is);
			} catch (Exception e) {
				return null;
			}

			return appConfig;

		} else {
			return null;
		}

	}

	public static XSMSConfig decodeSMSConfig(Context pContext) {
		XSMSConfig smsConfig = null;
		InputStream is = GNetworkUtils.doExcuteByGetWithCheckingWifi(pContext, CONFIG_SMS_URL);
		if (is != null) {
			Serializer serializer = new Persister();
			try {
				smsConfig = serializer.read(XSMSConfig.class, is);
			} catch (Exception e) {

				e.printStackTrace();
			}
		}
		return smsConfig;
	}

	public static String getAppInfo(Context pContext) {
		if (APP_INFO == null) {
			InputStream is = null;
			try {
				is = pContext.getAssets().open(APP_INFO_PATH);
				try {
					APP_INFO = convertStreamToString(is);
				} catch (Exception e) {

					e.printStackTrace();
				}
			} catch (IOException e1) {
				return null;
			}
		}
		return APP_INFO;
	}

	public static String convertStreamToString(InputStream is) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;

		while ((line = reader.readLine()) != null) {
			sb.append(line);
		}

		is.close();

		return sb.toString();
	}
}
